package com.tg.app.activity.device.add;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;
import com.tg.app.helper.ActivityHelper;
import com.appbase.custom.config.ApiUrl;
import com.tg.app.activity.CloudServiceActivity;
import com.tg.app.activity.base.WebBaseActivity;
import com.tg.appcommon.android.TGToast;
import com.tg.data.bean.DeviceSettingsInfo;
import com.appbase.custom.constant.CommonConstants;
import com.tange.base.toolkit.StatusBarUtil;
import com.tg.app.R;
import java.lang.reflect.Method;
import static com.tg.app.activity.device.DeviceSettingsActivity.EXT_DEVICE_INFO;

/**
 * 通用tip的activity实现
 * Created by zhangqiang on 2019/7/3
 * Copyright (c) 2018 Xunlei. All rights reserved.
 */
public class TipDialogActivity extends FragmentActivity {
    private long mDeviceId;
    private String mIccId;
    private int mType ;
    public static final String DIALOG_TYPE ="dialog_type";

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setStatusBarTransparent(this);
        mDeviceId = getIntent().getLongExtra(ApiUrl.DEVICEID, 0);
        mIccId = getIntent().getStringExtra(ApiUrl.DEVICE_AUTH);
        mType = getIntent().getIntExtra(DIALOG_TYPE,0);
        setContentView(R.layout.layout_tip_dialog_content);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        translucentActivity(this);
        initView();
    }

    private void translucentActivity(Activity activity) {
        try {
            activity.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            activity.getWindow().getDecorView().setBackground(null);
            Method activityOptions = Activity.class.getDeclaredMethod("getActivityOptions");
            activityOptions.setAccessible(true);
            Object options = activityOptions.invoke(activity);

            Class<?>[] classes = Activity.class.getDeclaredClasses();
            Class<?> aClass = null;
            for (Class clazz : classes) {
                if (clazz.getSimpleName().contains("TranslucentConversionListener")) {
                    aClass = clazz;
                }
            }
            Method method = Activity.class.getDeclaredMethod("convertToTranslucent",
                    aClass, ActivityOptions.class);
            method.setAccessible(true);
            method.invoke(activity, null, options);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    private void initView(){
        findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(TipDialogActivity.this, CloudServiceActivity.class);
//                DeviceSettingsInfo info = new DeviceSettingsInfo();
//                info.deviceID = mDeviceId;
//                intent.putExtra(WebBaseActivity.KEY_WEBURL, ApiUrl.AUTHENTICATION_PAGE + mIccId);
//                intent.putExtra(CloudServiceActivity.EXT_CLOUD_SERVICE_TYPE, 0);
//                intent.putExtra(WebBaseActivity.KEY_WEBTITLE, getString(R.string.web_auth_title));
//                intent.putExtra(EXT_DEVICE_INFO, info);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                startActivity(intent);
//                finish();

                TGToast.showToast("该功能暂未开放");
            }
        });
        TextView textView =findViewById(R.id.text_cancel);
        if (mType ==1){
            textView.setText(R.string.sim_auth_completed);
        }
        findViewById(R.id.text_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mType==1){
                    Intent intent = new Intent();
                    intent.putExtra(CommonConstants.EXT_DEVICE_ID,mDeviceId);
                    setResult(RESULT_OK,intent);
                }else {
                    ActivityHelper.gotoDeviceListPage(TipDialogActivity.this);
                }
                finish();
            }
        });

    }

}
